import { WXShowToast, WXRedirectTo } from '../../utils/index.js'
import { createNewAddress, Api } from '../../config/api.js';
import { request } from '../../utils/index.js'
// pages/new_addr/new_addr.js
Page({
  data: {
    name: '',
    phone: '',
     region: ['广东省', '广州市', '海珠区'],
    street: '',
    address: ''
  },
  onLoad: function (options) {
      
  },
  onShow(){
    
  },
  bindRegionChange(e) {
    this.setData({
      region: e.detail.value
    })
  },
  bindFieldChange(e) {
    const key = e.currentTarget.dataset.name
    const value = e.detail.value
    this.setData({
      [key]: value
    })
  },
  isValidForm() {
    const { name, phone, street, address } = this.data
    if (!name || !phone || !street || !address) return false
    if (!(/^1[3456789]\d{9}$/.test(phone))) return false

    return true
  },


//   添加收货地址

async   handleSave() {
    if (!this.isValidForm()) {
      WXShowToast('填写有误')
      return
    }


    const data = {
      province: this.data.region[0],
      city: this.data.region[1],
      county: this.data.region[2],
      street: this.data.street,
      detailed_address: this.data.address,
      phone:this.data.phone,
      name:this.data.name
    }

    const res = await request({
        url: Api.addNewAddress,
         type: 'POST',
         data
    })
    if(res.data.code==2001){
        wx.navigateBack({
            delta: 1
        });
    }
  } ,

})